<?php
//error_reporting(E_ALL ^ E_NOTICE);//TMP


if ( ! defined('EXT') ) exit('Invalid file request');

/**
* Manages extension activation, deactivation 
* 
* @author Alex Glover
*/

class Nce_html_output_tweaker {
	
	/**
	* Extension settings
	* @var array
	*/
	var $settings			= array();

	/**
	* Extension name
	* @var string
	*/
	var $name				= 'HTML output tweaker';

	/**
	* Extension version
	* @var string
	*/
	var $version			= '1.0.0';

	/**
	* Extension description
	* @var string
	*/
	var $description		= 'Tweaks HTML outputted by EE whenever a page is shown';

	/**
	* If $settings_exist = 'y' then a settings page will be shown in the ExpressionEngine admin
	* @var string
	*/
	var $settings_exist		= 'y';

	/**
	* Link to extension documentation
	* @var string
	*/
	var $docs_url = '';

	/**
	* Debug
	* @var array
	*/
	var $debug = FALSE;
//	var $debug = TRUE;

//	var $globals_replacer = null;

	/**
	* PHP4 Constructor
	*
	* @see __construct()
	*/
	function Nce_html_output_tweaker($settings='')
	{
		$this->__construct($settings);
	}


	/**
	* PHP5 Constructor
	*
	* @param	array|string $settings Extension settings associative array or an empty string
	*/
	function __construct($settings='')
	{
		global $SESS;
		$this->settings = $settings;
				
//		$this->globals_replacer = new Global_variable_replacer();
		
		if ($this->debug)
		{
//			print("<pre>Globals:\n");//TMP
//			print_r($this->globals_replacer->globals_lookup);//TMP
//			print("</pre>");//TMP			
		}
	}


	/**
	* Configuration for the extension settings page
	*
	* @return	array The settings array
	*/
	function settings()
	{
		$settings = array();
		$settings['orphan_replacement'] = array('s', array('0' => 'No', '1' => 'Yes'), '1');
/*
		$settings['twitter_password']	= '';
		$settings['summary_form_field'] = '';// NEW
		$settings['body_form_field'] = '';// NEW
		$settings['site_root_url'] = ''; // NEW
		$settings['use_tiny_url'] = array('s', array('0' => 'No', '1' => 'Yes'), '0'); // NEW
*/		
		return $settings;
	}



	/**
	* Activates the extension
	* 
	* @return	bool Always TRUE
	*/
	function activate_extension ()
	{
		global $DB, $PREFS;

		$default_settings = serialize(
			array(
				'orphan_replacement'		=> 1,
			)
		);

		$hooks = array(
			'typography_parse_type_end'		 => 'typography_parse_type_end',
		);

		foreach ($hooks as $hook => $method)
		{
			$sql[] = $DB->insert_string( 'exp_extensions', 
											array('extension_id' 	=> '',
												'class'			=> get_class($this),
												'method'		=> $method,
												'hook'			=> $hook,
												'settings'		=> $default_settings,
												'priority'		=> 10,
												'version'		=> $this->version,
												'enabled'		=> "y"
											)
										);
		}
				
		// run all sql queries
		foreach ($sql as $query)
		{
			$DB->query($query);
		}
		
		return TRUE;
	}



	/**
	* Updates the extension
	*/
	function update_extension($current = '')
	{
		global $DB;
		if ($current == '' OR $current == $this->version) return FALSE;
		
		$sql[] = "UPDATE exp_extensions SET version = '" . $DB->escape_str($this->version) . "' WHERE class = '" . get_class($this) . "'";

		// run all sql queries
		foreach ($sql as $query)
		{
			$DB->query($query);
		}
	}



	/**
	* Disables the extension
	*
	* Generally this method would activate the extension however this 
	* automatically happens when we activate the module. As a result this method
	* displays an error message to the user.
	*
	* @return	bool Always TRUE
	*/
	function disable_extension()
	{
		global $DB;
		$DB->query("DELETE FROM exp_extensions WHERE class = '" . get_class($this) . "'");
	}
		
	/**
	*/
	function typography_parse_type_end($str, $typography_object, $prefs)
	{
		global $IN, $OUT, $SESS,$LANG, $LOC, $PREFS;
		
		$output = $str;

		if ($this->settings['orphan_replacement'])
		{
			$regex = "([^ \n><]+) +([^ \n><]+[ \n]*((</[\w]+>)*.(</p>)))";
			$replacement = "\\1&nbsp;\\2";
		
			$output = preg_replace("'".$regex."'siU", $replacement, $output);
			if ($this->debug)
			{
				print("replaced: $str\nwith: $output\n\n");//TMP
			}
		}
		
		return $output;
	}
}


?>